package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsMallService;
import com.qf.vo.GenVo;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * projectName:temp_shop
 * author:luo
 * time:2023/2/24 15:51
 */
@Service
public class DtsMallServiceImpl implements DtsMallService {
    @Autowired
    private DtsRegionMapper dtsRegionMapper;
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Autowired
    private DtsAdminMapper dtsAdminMapper;
    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Autowired
    private DtsOrderMapper dtsOrderMapper;
    @Autowired
    private DtsOrderGoodsMapper dtsOrderGoodsMapper;
    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Autowired
    private DtsIssueMapper dtsIssueMapper;
    @Autowired
    private DtsKeywordMapper dtsKeywordMapper;

    @Override
    public IPage<DtsRegion> findRegionList(Integer page, Integer limit, String name, String code) {
        return dtsRegionMapper.selectPage(new Page<>(page,limit),
                new QueryWrapper<DtsRegion>()
                        .eq(!StringUtils.isEmpty(code),"code",code)
                        .like(!StringUtils.isEmpty(name),"name",name));
    }

    @Override
    public IPage<DtsBrand> findBrandList(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        return dtsBrandMapper.selectPage(new Page<>(page,limit),
                new QueryWrapper<DtsBrand>()
                        .eq(!StringUtils.isEmpty(id),"id",id)
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .orderBy(!StringUtils.isEmpty(sort),"DESC".equalsIgnoreCase(order),sort));
    }

    @Override
    public List<GenVo> findAdminList() {
        return dtsAdminMapper.selectList(null).stream().map(dtsAdmin -> {
            GenVo genVo = new GenVo();
            genVo.setValue(dtsAdmin.getId());
            genVo.setLabel(dtsAdmin.getUsername());
            return genVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CatVo> findCategoryList() {
        return dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>()
                .eq("pid",0)).stream().map(dtsCategory -> {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());
            List<CatVo> children = dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>().eq("pid", dtsCategory.getId())).stream()
                    .map(dtsCategory1 -> {
                        CatVo catVo1 = new CatVo();
                        catVo1.setValue(dtsCategory1.getId());
                        catVo1.setLabel(dtsCategory1.getName());
                        catVo1.setChildren(null);
                        return catVo1;
                    }).collect(Collectors.toList());
            catVo.setChildren(children);
            return catVo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addBrand(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrandMapper.insert(dtsBrand);
    }

    @Override
    public void modifyBrand(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
    }

    @Override
    public void removeBrand(DtsBrand dtsBrand) {
        dtsBrandMapper.deleteById(dtsBrand);
    }

    @Override
    public IPage<DtsCategory> findCategoryListAll(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        return dtsCategoryMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsCategory>()
                .eq(!StringUtils.isEmpty(id),"id",id)
                .like(!StringUtils.isEmpty(name),"name",name)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public List<CatVo> findCategoryByOne() {
        return dtsCategoryMapper.selectList(new QueryWrapper<DtsCategory>()
                .eq("pid",0)).stream().map(dtsCategory -> {
            CatVo categoryVo = new CatVo();
            categoryVo.setValue(dtsCategory.getId());
            categoryVo.setLabel(dtsCategory.getName());
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addCategory(DtsCategory dtsCategory) {
        dtsCategory.setAddTime(new Date());
        dtsCategoryMapper.insert(dtsCategory);
    }

    @Override
    public void modifyCategory(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryMapper.updateById(dtsCategory);
    }

    @Override
    public void removeCategory(DtsCategory dtsCategory) {
        dtsCategoryMapper.deleteById(dtsCategory);
    }

    @Override
    public IPage<DtsOrder> findOrderList(Integer page, Integer limit, String sort, String order, Integer userId, String orderSn, Integer[] orderStatusArray) {
        return dtsOrderMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsOrder>()
                .eq(!StringUtils.isEmpty(userId),"user_id",userId)
                .like(!StringUtils.isEmpty(orderSn),"order_sn",orderSn)
                .in(!StringUtils.isEmpty(orderStatusArray),"order_status", orderStatusArray)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public void addShip(Integer id, String shipSn, String shipChannel) {
        DtsOrder dtsOrder = new DtsOrder();
        dtsOrder.setOrderStatus(402);
        dtsOrder.setId(id);
        dtsOrder.setShipSn(shipSn);
        dtsOrder.setShipChannel(shipChannel);
        dtsOrderMapper.updateById(dtsOrder);
    }

    @Override
    public List<DtsOrderGoods> findDetailListById(Integer id) {
        return dtsOrderGoodsMapper.selectList(new QueryWrapper<DtsOrderGoods>().eq("order_id",id));
    }

    @Override
    public DtsUser findUserOrderId(Integer id) {
        DtsOrder dtsOrder = dtsOrderMapper.selectOne(new QueryWrapper<DtsOrder>().eq("id", id));
        return dtsUserMapper.selectOne(new QueryWrapper<DtsUser>().eq("id", dtsOrder.getUserId()));
    }

    @Override
    public DtsOrder findOrderListById(Integer id) {
        return dtsOrderMapper.selectOne(new QueryWrapper<DtsOrder>().eq("id",id));
    }

    @Override
    public IPage<DtsIssue> findQuestionList(Integer page, Integer limit, String sort, String order, String question) {
        return dtsIssueMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsIssue>()
                .like(!StringUtils.isEmpty(question),"question",question)
                .orderBy(!StringUtils.isEmpty(question),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public void addIssue(DtsIssue dtsIssue) {
        dtsIssue.setAddTime(new Date());
        dtsIssueMapper.insert(dtsIssue);
    }

    @Override
    public void modifyIssue(DtsIssue dtsIssue) {
        dtsIssue.setUpdateTime(new Date());
        dtsIssueMapper.updateById(dtsIssue);
    }

    @Override
    public void removeIssue(DtsIssue dtsIssue) {
        dtsIssueMapper.deleteById(dtsIssue);
    }

    @Override
    public IPage<DtsKeyword> findKeywordList(Integer page, Integer limit, String sort, String order, String keyword, String url) {
        return dtsKeywordMapper.selectPage(new Page<>(page,limit),new QueryWrapper<DtsKeyword>()
                .like(!StringUtils.isEmpty(keyword),"keyword",keyword)
                .like(!StringUtils.isEmpty(url),"url",url)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    @Override
    public void addKeyword(DtsKeyword dtsKeyword) {
        dtsKeyword.setAddTime(new Date());
        dtsKeywordMapper.insert(dtsKeyword);
    }

    @Override
    public void modifyKeyword(DtsKeyword dtsKeyword) {
        dtsKeyword.setUpdateTime(new Date());
        dtsKeywordMapper.updateById(dtsKeyword);
    }

    @Override
    public void removeKeyword(DtsKeyword dtsKeyword) {
        dtsKeywordMapper.deleteById(dtsKeyword);
    }


}
